home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-24 | 9.4 KB | 394 lines | [TEXT/MPS ] |
- // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
- // UShapes.cp
-
- #ifndef __UTEXTSHAPE__
- #include "UTextShape.h"
- #endif
-
- // MacApp
-
- #ifndef __UDIALOG__
- #include <UDialog.h>
- #endif
-
- #ifndef __UFILE__
- #include <UFile.h>
- #endif
-
- #ifndef __USTREAM__
- #include <UStream.h>
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include <UMacAppUtilities.h>
- #endif
-
- #ifndef __UVIEW__
- #include <UView.h>
- #endif
-
- #ifndef __UMEMORY__
- #include <UMemory.h>
- #endif
-
- // Toolbox
-
- #ifndef __QUICKDRAW__
- #include <Quickdraw.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- // DrawShapes
-
- #ifndef __PATTERNMENU__
- #include "PatternMenu.h"
- #endif
-
- #ifndef __UDRAWSHAPES__
- #include "UDrawShapes.h"
- #endif
-
- #ifndef __USHAPEVIEW__
- #include "UShapeView.h"
- #endif
-
- //----------------------------------------------------------------------------------------
-
- const Boolean kUseOutlineFonts = true;
-
- const short kEditTextInset = 4;
-
- //========================================================================================
- // GLOBAL Functions
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // HandleToString
- //----------------------------------------------------------------------------------------
- #pragma segment AClipboard
-
- void HandleToString(Handle text, CStr255& string)
- {
- short textLength = Min(GetHandleSize(text), 255);
- string.Length() = textLength;
- if (textLength > 0)
- MABlockMove(*text, &string[1], textLength);
- }
-
- //----------------------------------------------------------------------------------------
- // StringToHandle
- //----------------------------------------------------------------------------------------
- #pragma segment AClipboard
-
- void StringToHandle(CStr255& string, Handle text)
- {
- Size textLength = string.Length();
- if (GetHandleSize(text) != textLength)
- {
- SetPermHandleSize(text, textLength);
- FailMemError();
- }
- if (textLength > 0)
- MABlockMove(&string[1], *text, textLength);
- }
-
- //========================================================================================
- // CLASS TTextShape
- //========================================================================================
- #undef Inherited
- #define Inherited TShape
-
- #pragma segment ARes
- MA_DEFINE_CLASS_M1(TTextShape, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TTextShape Constructor
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- TTextShape::TTextShape()
- {
- fEditText = NULL;
- fText = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::ITextShape
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TTextShape::ITextShape(const CRect& itsExtent, short itsID)
- {
- IShape(itsExtent, itsID);
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::DoInitialState
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TTextShape::DoInitialState(TShapeView* itsView)
- {
- Inherited::DoInitialState(itsView);
-
- CreateEditText(itsView);
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::CreateEditText
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TTextShape::CreateEditText(TView* itsView)
- {
- if (!fEditText)
- {
- VPoint loc(fLocation);
- VPoint size(fSize);
- VPoint inset(kEditTextInset, kEditTextInset);
- loc += inset;
- size -= inset;
- size -= inset;
-
- fEditText = new TEditText;
- fEditText->IEditText(itsView, loc, size, 255);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::SetEditTextFrame
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TTextShape::SetEditTextFrame()
- {
- if (fEditText)
- {
- VPoint loc(fLocation);
- VPoint size(fSize);
- VPoint inset(kEditTextInset, kEditTextInset);
- loc += inset;
- size -= inset;
- size -= inset;
- VRect newFrame(loc, loc + size);
- fEditText->SetFrame(newFrame, kInvalidate);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::Clone
- //----------------------------------------------------------------------------------------
- #pragma segment AClose
-
- TObject* TTextShape::Clone() // Override
- {
- TTextShape* bozo = (TTextShape*) Inherited::Clone();
- FailNIL(bozo);
-
- bozo->fEditText = NULL;
- bozo->fText = NULL;
-
- if (fText)
- {
- Size textSize = GetHandleSize(fText);
- Handle textData = NewPermHandle(textSize);
- FailNIL(textData);
- MABlockMove(*fText, *textData, textSize);
- bozo->fText = textData;
- }
-
- return bozo;
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::Free
- //----------------------------------------------------------------------------------------
- #pragma segment AClose
-
- void TTextShape::Free() // Override
- {
- fEditText = NULL; // (TEditText*) FreeIfObject(fEditText);
- fText = NULL; // DisposeIfHandle(fText);
-
- Inherited::Free();
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::ReadFrom
- //----------------------------------------------------------------------------------------
- #pragma segment AReadFile
-
- void TTextShape::ReadFrom(TStream* aStream) // Override
- {
- Inherited::ReadFrom(aStream);
-
- Handle text = aStream->ReadHandle();
- SetText(text);
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::WriteTo
- //----------------------------------------------------------------------------------------
- #pragma segment AWriteFile
-
- void TTextShape::WriteTo(TStream* aStream) // Override
- {
- Inherited::WriteTo(aStream);
-
- if (fEditText)
- {
- CStr255 string;
- fEditText->GetText(string);
- if (!fText)
- {
- fText = NewPermHandle(string.Length());
- FailNIL(fText);
- }
- StringToHandle(string, fText);
- }
- aStream->WriteHandle(fText);
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::SetText
- //----------------------------------------------------------------------------------------
- #pragma segment AClipboard
-
- void TTextShape::SetText(Handle text)
- {
- if (fText != text)
- {
- fText = DisposeIfHandle(fText);
- fText = text;
- }
-
- if (!fEditText)
- CreateEditText(NULL);
-
- if (fEditText)
- {
- CStr255 string;
- HandleToString(fText, string);
- fEditText->SetText(string, kDontRedraw);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::BeInView
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TTextShape::BeInView(TShapeView* itsView) // Override
- {
- // Inherited::BeInView(itsView);
-
- if (!fEditText)
- CreateEditText(itsView);
- else
- itsView->AddSubView(fEditText);
-
- if (fText)
- SetText(fText);
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::Draw
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TTextShape::Draw() // Override
- {
- CRect extent;
- GetFrame(extent);
-
- PenNormal();
- if (qNeedsColorQD || gConfiguration.hasColorQD)
- {
- // Get the color of the menu item representing the shape's color
- RGBForeColor(fColor);
- DrawInsides(extent);
- ForeColor(blackColor);
- }
- else
- DrawInsides(extent);
-
- if (!fEditText)
- {
- const CStr255 s = "T";
-
- TextFont(geneva);
- TextFace(normal);
- TextSize(9);
- TextMode(srcOr);
-
- short x = (extent.left + extent.right) / 2;
- short w = StringWidth(s) / 2;
- CRect r(x - w, extent.bottom - 14, x + w, extent.bottom - 4);
- EraseRect(r);
- MADrawString(s, r, teCenter, kUseOutlineFonts);
- }
-
- DrawOutline();
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::DrawInsides
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TTextShape::DrawInsides(const CRect& extent)
- {
- CRect r(extent);
-
- r.bottom = extent.top + kEditTextInset;
- FillRect(r, &gPat[fPattern]);
- r.bottom = extent.bottom;
-
- r.right = extent.left + kEditTextInset;
- FillRect(r, &gPat[fPattern]);
- r.right = extent.right;
-
- r.left = extent.right - kEditTextInset;
- FillRect(r, &gPat[fPattern]);
- r.left = extent.left;
-
- r.top = extent.bottom - kEditTextInset;
- FillRect(r, &gPat[fPattern]);
- r.top = extent.top;
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::DrawOutline
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TTextShape::DrawOutline()
- {
- PenSize(1, 1);
- CRect extent;
- GetFrame(extent);
- FrameRect(extent);
- }
-
- //----------------------------------------------------------------------------------------
- // TTextShape::SetFrame
- //----------------------------------------------------------------------------------------
- #pragma segment AClipboard
-
- void TTextShape::SetFrame(const CRect& extentRect) // override
- {
- CRect oldFrame;
- GetFrame(oldFrame);
- if (oldFrame != extentRect)
- {
- Inherited::SetFrame(extentRect);
- if (fEditText)
- SetEditTextFrame();
- }
- }
-
-